package com.evl.station.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.evl.common.controller.BaseController;
import com.evl.common.exception.BizExceptionEnum;
import com.evl.common.exception.BussinessException;
import com.evl.common.persistence.dao.StationMapper;
import com.evl.common.persistence.dao.VehicleMapper;
import com.evl.common.persistence.model.Station;
import com.evl.common.persistence.model.Vehicle;
import com.evl.core.log.LogObjectHolder;
import com.evl.core.util.ToolUtil;
import com.evl.station.service.IStationService;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yangliwei
 * @since 2017-06-14
 */
@Controller
@RequestMapping("/station")
public class StationController  extends BaseController{
	private static String PREFIX = "/station";
	
	@Resource
	VehicleMapper vehicleMapper;
	
	@Resource
	StationMapper stationMapper;
	
	@Resource
	IStationService stationService;
	
	 /**
     * 跳转到车站管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/station.html";
    }

    /**
     * 跳转到添加车站管理
     */
    @RequestMapping("/station_add/{vehicleId}")
    public String stationAdd(@PathVariable Integer vehicleId, Model model) {
    	Vehicle vehicle = vehicleMapper.selectById(vehicleId);
    	model.addAttribute("vehicle", vehicle);
        return PREFIX + "/station_add.html";
    }
	
    /**
     * 跳转到修改车站管理
     */
    @RequestMapping("/station_update/{stationId}")
    public String stationUpdate(@PathVariable Integer stationId, Model model) {
    	Station station = this.stationMapper.selectById(stationId);
    	model.addAttribute(station);
    	 LogObjectHolder.me().set(station);
        return PREFIX + "/station_edit.html";
    }

    /**
     * 获取车站管理列表
     */
    @RequestMapping(value = "/list/{vehicleId}")
    @ResponseBody
    public Object list(@PathVariable Integer vehicleId,@RequestParam(required = false) String stationName) {
    	
    	 List<Station> result = stationMapper.selectStationByWhere(vehicleId);
    	 
    	 return result;
    }

    /**
     * 新增车站管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Station station) {
    	if(ToolUtil.isOneEmpty(station,station.getStationName())){
    		throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
    	}
    	return this.stationMapper.insert(station);
    }

    /**
     * 删除车站管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer stationId) {
    	
        return this.stationMapper.deleteById(stationId);
    }


    /**
     * 修改车站管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Station station) {
    	 if (ToolUtil.isEmpty(station) || station.getId() == null) {
             throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
         }
        return this.stationMapper.updateById(station);
    }

    /**
     * 车站管理详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail(@RequestParam Integer stationId) {
        return this.stationMapper.selectById(stationId);
    }
}
